package src.main.java.org.netWorkProgram.TCP.demo1;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

/*
 * TCP连接——服务端 一发一收
 * */
public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("服务端启动成功");
        //1.创建ServerSocket对象，为服务端注册端口
        ServerSocket serverSocket = new ServerSocket(8888);

        //2.使用serverSocket对象，调用一个accept对象，等待客户端的连接请求
        Socket socket = serverSocket.accept();

        //3.从socket通信管道得到字节输入流
        InputStream is = socket.getInputStream();
        //4.原始字节输入流包装成数据输入流
        DataInputStream dataInputStream = new DataInputStream(is);
        //5.读取数据
        String s = dataInputStream.readUTF();
        System.out.println(s);
        //6.获取客户端IP地址
        SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress();
        System.out.println(remoteSocketAddress);

        dataInputStream.close();
        socket.close();
    }
}
